package com.simple.core.log;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.simple.annotation.apt.Simpler;
import com.simple.core.util.IPHelper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.Where;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 系统日志;
 *
 * @author : Clarence
 * @date : 2023-1-12
 */

@Simpler(prefix = "SysLog")
@Data
@NoArgsConstructor
@ApiModel(description = "系统日志")
@Entity
@Table(name = "sys_log")
@Where(clause = "deleted = 0")
@EntityListeners(AuditingEntityListener.class)
public class SysLogs implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "自增主键", notes = "")
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ApiModelProperty(value = "请求ID", notes = "")
    private String requestId;

    @ApiModelProperty(value = "请求函数Lambda", notes = "")
    private String method;

    @ApiModelProperty(value = "模块描述", notes = "")
    private String module;

    @ApiModelProperty(value = "操作描述", notes = "")
    private String description;

    @ApiModelProperty(value = "操作状态", notes = "")
    private Boolean status;

    @ApiModelProperty(value = "异常信息", notes = "")
    private String errorMsg;

    @ApiModelProperty(value = "执行时长", notes = "")
    private Integer executeTime;

    @ApiModelProperty(value = "请求地址ip", notes = "")
    private String ip;

    @ApiModelProperty(value = "操作人", notes = "")
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @CreatedBy
    @Column(name = "operator")
    private String createdBy;

    @ApiModelProperty(value = "创建时间", notes = "")
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @CreationTimestamp
    private LocalDateTime createdTime;
    @ApiModelProperty("创建时间-开始")
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private transient Date createdTimeStart;
    @ApiModelProperty("创建时间-结束")
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private transient Date createdTimeEnd;

    @JsonIgnore
    private Boolean deleted = false;

    @JsonIgnore
    @ApiModelProperty(value = "租户号", notes = "")
    private Long tenantId;

    public SysLogs(String requestId, String method, String module, String description, Boolean status, String errorMsg, Integer executeTime) {
        this.requestId = requestId;
        this.method = method;
        this.module = module;
        this.description = description;
        this.status = status;
        this.errorMsg = errorMsg;
        this.executeTime = executeTime;
        this.ip = IPHelper.getIpAddr();
    }

}

